package com.gnizr.feed ;

import com.gnizr.feed.FeedDao ;

import java.io.Serializable ;
import java.lang.Comparable ;
import java.util.Date ;
import java.util.List ;
import java.text.DateFormat ;
import java.text.SimpleDateFormat ;

/**
 * Item in the Gnizr Newsfeed
 */
public class FeedItem implements Serializable, Comparable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 7440530893915763192L ;
	
	private int type ;
	private int id ;
	private String title ;
	private Date createdOn ;
	private Date lastUpdated ;
	private String username ;
	private String fullname ;
	private String link ;

	/**
	 * Creates a new instance of this class. 
	 */
	public FeedItem() {
                this.type = 0 ;
		this.title = "" ;
		this.username = "" ;
		this.fullname = "" ;
		this.link = "" ;
	}

    public int compareTo(Object other) throws ClassCastException
    {
        if (!(other instanceof FeedItem))
            throw new ClassCastException("A FeedItem object expected.");
        Date otherDate = ((FeedItem)other).getLastUpdated();  
        return lastUpdated.compareTo(otherDate) ;
    }

	public String toRSS()
	{
	    String desc = "" ;
	    switch ( type )
	    {
            case FeedDao.BOOKMARKS:
                if ( same() )
                    desc = fullname + " (" + username + ") has added new bookmark " + title + "." ;
                else
                    desc = fullname + " (" + username + ") has updated existing bookmark " + title + "." ;
                break ;
            case FeedDao.FOLDERS:
                    String tmp ;
                    if ( title.equals("_my_") )
                        tmp = "My Bookmarks" ;
                    else
                        tmp = title ;
                    desc = fullname + " (" + username + ") has created/updated folder " + tmp + "." ;
                    link = "http://gnizr.emulate.us:8080/gnizr-feed/user/" + username + "/folder/" + title ;
                break ;
            case FeedDao.SUBSCRIPTIONS:
                    desc = fullname + " (" + username + ") has subscribed to " + title + "." ;
                break ;
            case FeedDao.USERS:
                    link = "http://gnizr.emulate.us:8080/gnizr-feed/user/" + username ;
                    desc = fullname + " (" + username + ") has joined the Gnizr community." ;
                break ;
            case FeedDao.TAGS:
                link = "http://gnizr.emulate.us:8080/gnizr-feed/tag/" + title ;
                if ( same() )
                    desc = fullname + " (" + username + ") has added new tag " + title + "." ;
                else
                    desc = fullname + " (" + username + ") has updated existing tag " + title + "." ;
                break ;
            default:
                break ;
        }

        SimpleDateFormat fmt =
            new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz") ;
	    String lastUpdated2 = fmt.format(lastUpdated) ;

	    String message = "" ;
	    message = message + "<item>\n" ;
	    message = message + "<title>" + title + "</title>\n" ;
	    message = message + "<link>" + link + "</link>\n" ;
	    message = message + "<description>" + desc + "</description>\n" ;
	    message = message + "<pubDate>" + lastUpdated2 + "</pubDate>\n" ;
	    message = message + "</item>\n" ;

	    return message ;    
    }
	
	public String toString()
	{
	    switch ( type )
	    {
            case FeedDao.BOOKMARKS:
                if ( same() )
                    return "<li class=bookmark>" + fullname + " (<a href='/gnizr-feed/user/" + username + "'>" + username + "</a>) has added new bookmark <a href='" + link + "'>" + title + "</a>.</li>" ; 
                else
                    return "<li class=bookmark>" + fullname + " (<a href='/gnizr-feed/user/" + username + "'>" + username + "</a>) has updated existing bookmark <a href='" + link + "'>" + title + "</a>.</li>" ;
            case FeedDao.FOLDERS:
                    String tmp ;
                    if ( title.equals("_my_") )
                        tmp = "My Bookmarks" ;
                    else
                        tmp = title ;
                    return "<li class=folder>" + fullname + " (<a href='/gnizr-feed/user/" + username + "'>" + username + "</a>) has created/updated folder <a href='/gnizr-feed/user/" + username + "/folder/" + title + "'>" + tmp + "</a>.</li>" ;
            case FeedDao.SUBSCRIPTIONS:
                    return "<li class=feed>" + fullname + " (<a href='/gnizr-feed/user/" + username + "'>" + username + "</a>) has subscribed to <a href='" + link + "'>" + title + "</a>.</li>" ;
            case FeedDao.USERS:
                    return "<li class=user>" + fullname + " (<a href='/gnizr-feed/user/" + username + "'>" + username + "</a>) has joined the Gnizr community.</li>" ;
            case FeedDao.TAGS:
                if ( same() )
                    return "<li class=tag>" + fullname + " (<a href='/gnizr-feed/user/" + username + "'>" + username + "</a>) has added new tag <a href='/gnizr-feed/tag/" + title + "'>" + title + "</a>.</li>" ;
                else
                    return "<li class=tag>" + fullname + " (<a href='/gnizr-feed/user/" + username + "'>" + username + "</a>) has updated existing tag <a href='/gnizr-feed/tag/" + title + "'>" + title + "</a>.</li>" ;
            default:
                break ;
        }
        return "" ;
    }
	
	private Boolean same()
	{
	    return createdOn.equals(lastUpdated) ;
	}
	
	/**
	 * Returns the type of this FeedItem.
	 * @return FeedItem type.
	 */
	public int getType() {
		return type;
	}
	
	/**
	 * Sets the type of this FeedItem.
	 * @param type FeedItem type.
	 */
	public void setType(int type) {
		this.type = type;
	}
	
	/**
	 * Returns the ID of this FeedItem.
	 * @return FeedItem ID.
	 */
	public int getId() {
		return id;
	}
	
	/**
	 * Sets the ID of this FeedItem.
	 * @param id FeedItem ID.
	 */
	public void setId(int id) {
		this.id = id;
	}
	
	/**
	 * Returns the title of this FeedItem
	 * @return FeedItem title.
	 */
	public String getTitle() {
		return title;
	}
	
	/**
	 * Sets the title of this FeedItem
	 * @param title FeedItem title.
	 */
	public void setTitle(String title) {
		this.title = title;
	}
	
	/**
	 * Returns the date/time when the FeedItem is created.
	 * 
	 * @return the creation date/time of this FeedItem.
	 */
	public Date getCreatedOn() {
		return createdOn;
	}
	
	/**
	 * Sets the date/time when the FeedItem is created.
	 * 
	 * @param createdOn date/time when this FeedItem is created.
	 */
	public void setCreatedOn(Date createdOn) {
		this.createdOn = createdOn;
	}
	
	/**
	 * Returns the date/time when any properties of this FeedItem
	 * has been changed.
	 * 
	 * @return the last updated date/time of this FeedItem.
	 */
	public Date getLastUpdated() {
		return lastUpdated;
	}
	
	/**
	 * Sets the last updated date/time of this FeedItem.
	 * 
	 * @param lastUpdated when this FeedItem was last updated.
	 */
	public void setLastUpdated(Date lastUpdated) {
		this.lastUpdated = lastUpdated;
	}
	
	/**
	 * Returns the username of this FeedItem.
	 * @return FeedItem username.
	 */
	public String getUserName() {
		return username;
	}
	
	/**
	 * Sets the username of this FeedItem. 
	 * @param FeedItem username.
	 */
	public void setUserName(String username) {
		this.username = username;
	}

	/**
	 * Returns the fullname of this FeedItem.
	 * @return FeedItem fullname.
	 */
	public String getFullName() {
		return fullname;
	}
	
	/**
	 * Sets the fullname of this FeedItem. 
	 * @param FeedItem fullname.
	 */
	public void setFullName(String fullname) {
		this.fullname = fullname;
	}

	/**
	 * Returns the link of this FeedItem.
	 * @return FeedItem link.
	 */
	public String getLink() {
		return link;
	}
	
	/**
	 * Sets the link of this FeedItem. 
	 * @param link FeedItem link.
	 */
	public void setLink(String link) {
		this.link = link;
	}
}
